<?php
//商品分类控制器
class CategoryController extends BaseController {
	//显示商品分类
	public function indexAction(){
		//获取所有的商品分类数据
		$categoryModel = new CategoryModel('category');
		$cats = $categoryModel->getCats();
		include CUR_VIEW_PATH . "cat_list.html";
	}
	//显示添加表单页面
	public function addAction(){
		//获取所有的商品分类数据
		$categoryModel = new CategoryModel('category');
		$cats = $categoryModel->getCats();
		//载入视图
		include CUR_VIEW_PATH . "cat_add.html";
	}
	//完成添加动作
	public function insertAction(){
		//1.收集表单数据, 以关联数组的形式来收集
		$data['cat_name'] =trim($_POST['cat_name']);
		$data['unit'] =trim($_POST['unit']);
		$data['sort_order'] =trim($_POST['sort_order']);
		$data['cat_desc'] =trim($_POST['cat_desc']);
		$data['parent_id'] =trim($_POST['parent_id']);
		$data['is_show'] = $_POST['is_show'];

		//引入辅助函数
		$this->helper('input');
		//实体转义
		$data = deepslashes($data);
		$data = deepspecialchars($data);
		
		//实体转义
		// $data['cat_desc'] = htmlspecialchars($data['cat_desc']);

		//2.验证和处理
		if ($data['cat_name'] == '') {
			$this->jump('index.php?p=admin&c=category&a=add','分类名称不能为空');
		}
		//3.调用模型完成入库，并给出提示跳转
		$categoryModel = new CategoryModel('category');
		if ($categoryModel->insert($data)) {
			//ok
			$this->jump('index.php?p=admin&c=category&a=index','添加分类成功',2);
		} else {
			//failer
			$this->jump('index.php?p=admin&c=category&a=add','添加分类失败');
		}
	}
	//显示修改表单页面
	public function editAction(){
		//获取cat_id
		$cat_id = $_GET['cat_id'] + 0 ; //?思考
		//通过cat_id获取当前记录信息
		$categoryModel = new CategoryModel('category');
		$cat = $categoryModel->selectByPk($cat_id); //继承model中的selectByPk
		//获取所有的分类信息
		$categoryModel = new CategoryModel('category');
		$cats = $categoryModel->getCats();
		include CUR_VIEW_PATH . "cat_edit.html";
	}
	//完成更新操作
	public function updateAction(){
		//1.收集表单数据
		$data['cat_name'] =trim($_POST['cat_name']);
		$data['unit'] =trim($_POST['unit']);
		$data['sort_order'] =trim($_POST['sort_order']);
		$data['cat_desc'] =trim($_POST['cat_desc']);
		$data['parent_id'] =trim($_POST['parent_id']);
		$data['is_show'] = $_POST['is_show'];
		$data['cat_id'] = $_POST['cat_id']; //切记，不要忘了这一行

		//引入辅助函数
		$this->helper('input');
		//实体转义
		$data = deepslashes($data);
		$data = deepspecialchars($data);
		//2.验证和处理
		if ($data['cat_name'] == '') {
			$this->jump('index.php?p=admin&c=category&a=add','分类名称不能为空');
		}
		//3.调用模型完成更新操作，并提示跳转
		$categoryModel = new CategoryModel('category');
		//需要判断，如果选择的上级分类是当前分类及其后代，则不允许更新
		$ids = $categoryModel->getSubIds($data['cat_id']);
		if (in_array($data['parent_id'], $ids)) {
			$this->jump("index.php?p=admin&c=category&a=edit&cat_id={$data['cat_id']}",
				"不能将当前分类及其后代分类作为其上级分类");
		}
		if ($categoryModel->update($data)) {
			$this->jump('index.php?p=admin&c=category&a=index','更新分类成功',2);
		} else {
			$this->jump("index.php?p=admin&c=category&a=edit&cat_id={$data['cat_id']}",'更新分类失败');
		}
	}
	//删除操作
	public function deleteAction(){
		//获取cat_id
		$cat_id = $_GET['cat_id'] + 0;
		//调用模型完成删除，并提示跳转
		$categoryModel = new CategoryModel('category');
		//判断当前分类是否是最底层分类，如果不是，则提示不允许删除
		$ids = $categoryModel->getSubIds($cat_id);
		if (count($ids) > 1) {
			$this->jump('index.php?p=admin&c=category&a=index','当前分类不是最底层分类，请先删除子分类');
		}
		if ($categoryModel->delete($cat_id)) {
			$this->jump('index.php?p=admin&c=category&a=index','删除分类成功',2);
		} else {
			$this->jump('index.php?p=admin&c=category&a=index','删除分类失败');
		}
	}
}